package top.whysu.edevice.controller.manage;

import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import top.whysu.edevice.dto.generate.RequestDTO;
import top.whysu.edevice.dto.generate.ResponseDTO;
import top.whysu.edevice.dto.manage.SecretKeyAddModifyDTO;
import top.whysu.edevice.dto.manage.SecretKeyNameDTO;
import top.whysu.edevice.dto.manage.SecretKeyPageDTO;
import top.whysu.edevice.dto.manage.SecretKeyQueryDTO;
import top.whysu.edevice.dto.page.PagenationDataDTO;
import top.whysu.edevice.po.manage.ManageSecretKeyPO;
import top.whysu.edevice.service.manage.ManageSecretKeyService;
import top.whysu.edevice.utils.ResponseUtil;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * 秘钥管理
 */
@Controller
@RequestMapping("/manageSecretKey")
public class ManageSecretKeyController {

    @Resource
    private ManageSecretKeyService manageSecretKeyService;

    @RequestMapping("/manageSecretKey")
    public String deviceManager() {
        return "manage/manageSecretKey";
    }

    /**
     * 新增、修改秘钥
     */
    @PostMapping("/addModify")
    @ResponseBody
    public ResponseDTO addModifyDevice(@RequestBody @Validated RequestDTO<SecretKeyAddModifyDTO> requestDTO) {
        return ResponseUtil.getSuccessResponse(requestDTO.getHeader(), manageSecretKeyService.wholeAddModify(requestDTO.getBody()));
    }

    /**
     * 设备列表--二维表格
     */
    @RequestMapping("/getPage")
    @ResponseBody
    public ResponseDTO<PagenationDataDTO<SecretKeyPageDTO>> getPage(@RequestBody SecretKeyQueryDTO secretKeyQueryDTO) {
        PagenationDataDTO<SecretKeyPageDTO> page = manageSecretKeyService.selectPage(secretKeyQueryDTO);
        return ResponseUtil.getSuccessResponse(page);
    }

    /**
     * 删除秘钥
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseDTO delete(@RequestBody RequestDTO<BigDecimal> request) {
        manageSecretKeyService.delete(request.getBody());
        return ResponseUtil.getSuccessResponse();
    }

    /**
     * 通过凭证组树结构id查询其下的所有秘钥集合
     */
    @RequestMapping("/getSecretKeyListByVoucherTreeId")
    @ResponseBody
    public ResponseDTO<List<SecretKeyNameDTO>> getSecretKeyListByVoucherTreeId(@RequestBody RequestDTO<BigDecimal> request) {
        return ResponseUtil.getSuccessResponse(request.getHeader(), manageSecretKeyService.getSecretKeyListByVoucherTreeId(request.getBody()));
    }

    /**
     * 通过凭证组id查询其下的所有秘钥集合
     */
    @RequestMapping("/getSecretKeyListByVoucherId")
    @ResponseBody
    public ResponseDTO<List<SecretKeyNameDTO>> getSecretKeyListByVoucherId(@RequestBody RequestDTO<BigDecimal> request) {
        return ResponseUtil.getSuccessResponse(request.getHeader(), manageSecretKeyService.getSecretKeyListByVoucherId(request.getBody()));
    }
}